#include "STC8G.h"
#include "Key.h"

KEY_FIFO_T s_tKey;  /* 按键FIFO变量,结构体 */
KEY_T s_KEY[6];     // 按键结构体



void bsp_PutKey(uint8_t _KeyCode) {
    s_tKey.Buf[s_tKey.Write] = _KeyCode;
    s_tKey.Write++;

    if (s_tKey.Write >= KEY_FIFO_SIZE) {
        s_tKey.Write = 0;
    }
}

/*
*********************************************************************************************************
*  	函 数 名: bsp_GetKey
*  	功能说明: 从按键FIFO缓冲区读取一个键值。
*  	形    参: 无
*  	返 回 值: 按键代码
*********************************************************************************************************
*/
uint8_t bsp_GetKey(void) {
    uint8_t ret;

    if (s_tKey.Read == s_tKey.Write) {
        return KEYNUM_NULL;
    }
    else {
        ret = s_tKey.Buf[s_tKey.Read];
        s_tKey.Read++;
        if (s_tKey.Read >= KEY_FIFO_SIZE) {
            s_tKey.Read = 0;
        }
        return ret;
    }
}

void bsp_ClearKey(void) { s_tKey.Read = s_tKey.Write; }

/*
 * *************************************************
 *
 *
 * 自定义代码区域
 *
 *
 ***************************************************
 */


 // 按键初始化,设置为低电平有效
void bsp_keyInit(void) {
    unsigned char i;

    for (i = 0; i < 6; i++) {

        s_KEY[i].ACTIVE_LEVEL = 0;
    }
}

// keyInit();
#define KEY_KIND_NUM 6 // 按键种类数

void bsp_KeyDownAction(unsigned char i) {

    s_KEY[i].Count++;
    s_KEY[i].UpCount = 0;

    if ((s_KEY[i].State == 0) &&
        (s_KEY[i].Count == CNT_SHORT_MIN)) { // 按键按下时长首次达到短按门槛

        s_KEY[i].State = 1;    // 按键按下状态
        bsp_PutKey(KEY_KIND_NUM * i + 1); // 存入按键按下事件

    }
    else if ((s_KEY[i].Count >= CNT_LONG_MIN) &&
        (s_KEY[i].LongState == 0)) { // 按键按下时长首次达到长按门槛

        s_KEY[i].LongState = 1;                  // 长按状态
        s_KEY[i].Count = 0;
        bsp_PutKey(KEY_KIND_NUM * i + 3); // 存入按键长按事件
    }
}

void bsp_KeyUpAction(unsigned char i) {

    if (s_KEY[i].State) {

        s_KEY[i].UpCount++;
        if (s_KEY[i].UpCount >= CNT_UP_MIN) { // 按键抬起时长达到抬起门槛
            if (!s_KEY[i].LongState) {

                bsp_PutKey(KEY_KIND_NUM * i + 2); // 存入按键弹起事件


            }
            else {

                bsp_PutKey(KEY_KIND_NUM * i + 4); // 存入按键长按弹起事件

            }
            s_KEY[i].Count = 0;     // 清除按键按下时长计数
            s_KEY[i].State = 0;     // 按键抬起状态
            s_KEY[i].LongState = 0; // 清除长按状态
            s_KEY[i].UpCount = 0;   // 清除按键抬起时长计数
        }
    }
}

void bsp_KeyDownAction_MulTap(unsigned char i) {

    s_KEY[i].Count++;
    s_KEY[i].UpCount = 0;

    if ((s_KEY[i].State == 0) && (s_KEY[i].Count == CNT_SHORT_MIN)) { // 按键按下时长首次达到短按门槛

        s_KEY[i].State = 1;          // 按键按下状态
        bsp_PutKey(KEY_KIND_NUM * i + 1); // 存入按键按下事件

        if (s_KEY[i].MulTapCount == 0) {  // 第一次按下此按键
            s_KEY[i].MulTapCount = 1; // 开始进行按键按下时长计数
            s_KEY[i].MulTapTimes = 0;
            s_KEY[i].DoubleState = 0;
        }
        s_KEY[i].MulTapTimes++;

    }
    else if ((s_KEY[i].Count >= CNT_LONG_MIN) &&
        (s_KEY[i].LongState == 0)) {  // 按键按下时长首次达到长按门槛
        s_KEY[i].LongState = 1; // 长按状态
        s_KEY[i].Count = 0;
        if (s_KEY[i].DoubleState == 0) { // 无连击
            bsp_PutKey(KEY_KIND_NUM * i + 3); // 存入按键长F按事件
        }
    }

}

void bsp_KeyUpAction_MulTap(unsigned char i) {


    if (s_KEY[i].State) {

        s_KEY[i].UpCount++;
        if (s_KEY[i].UpCount >= CNT_UP_MIN) { // 按键抬起时长达到抬起门槛
            if (!s_KEY[i].LongState) {
                bsp_PutKey(KEY_KIND_NUM * i + 2); // 存入短按的按键弹起事件
                if ((s_KEY[i].MulTapTimes == 1) && (s_KEY[i].MulTapCount == 0)) { //如果不在双击判断范围，且连击记录为1
                    bsp_PutKey(KEY_KIND_NUM * i + 5);  //存入单击事件
                    s_KEY[i].MulTapTimes = 0;
                }
            }
            else {
                s_KEY[i].MulTapTimes = 0;
                if (!s_KEY[i].DoubleState) {
                    // bsp_PutKey(KEY_KIND_NUM * i + 2); // 存入按键弹起事件
                    bsp_PutKey(KEY_KIND_NUM * i + 4); // 存入按键长按弹起事件
                }

            }
            s_KEY[i].Count = 0;     // 清除按键按下时长计数
            s_KEY[i].State = 0;     // 按键抬起状态
            s_KEY[i].LongState = 0; // 清除长按状态
            s_KEY[i].UpCount = 0;   // 清除按键抬起时长计数
        }
    }
}

void bsp_KeyScanHandler_MulTap(unsigned char i) {
    if (s_KEY[i].MulTapCount > 0) {
        s_KEY[i].MulTapCount++;
        if (s_KEY[i].MulTapCount >= CNT_MulTap_INTERVAL) {    //双击计时周期统计结算
            s_KEY[i].MulTapCount = 0;            // 计数清零
            if (s_KEY[i].MulTapTimes > 1) {

                bsp_PutKey(KEY_KIND_NUM * i + 6); // 存入按键双击事件
                s_KEY[i].MulTapTimes = 0;
                s_KEY[i].DoubleState = 1;

            }
            else if (s_KEY[i].State == 0) {   // 按键已经抬起且没有双击动作
                bsp_PutKey(KEY_KIND_NUM * i + 5); // 存入按键单击事件
                s_KEY[i].MulTapTimes = 0;

            }

        }
    }




}


/**
 * @brief 按键扫描处理函数@5ms
 *
 */
void bsp_KeyScan(void) {
    //同时按下数位两个按键
    if ((IN_KEY1 == 0) && (IN_KEY2 == 0)) { // 检测按键按下
        bsp_KeyDownAction(0);
    }
    else {
        bsp_KeyUpAction(0); // 检测按键弹起


    }

    if ((IN_KEY1 == 0) && (IN_KEY2 != 0)) {   // 检测按键按下

        bsp_KeyDownAction_MulTap(1);
    }
    else {
        bsp_KeyUpAction_MulTap(1);                     // 检测按键弹起
    }
    if ((IN_KEY2 == 0)&&(IN_KEY1 != 0)) {   // 检测按键按下

        bsp_KeyDownAction_MulTap(2);
    }
    else {
        bsp_KeyUpAction_MulTap(2);                     // 检测按键弹起
    }

    if (IN_KEY3 == 0) {   // 检测按键按下
        bsp_KeyDownAction_MulTap(3);
    }
    else {
        bsp_KeyUpAction_MulTap(3);                   // 检测按键弹起
    }





    // bsp_KeyScanHandler_MulTap(0);
    bsp_KeyScanHandler_MulTap(1);
    bsp_KeyScanHandler_MulTap(2);
    bsp_KeyScanHandler_MulTap(3);
}

/**
 * @brief 照明旋钮档位扫描
 *
 * @return unsigned char
 */
unsigned char bsp_Selector_Scan_ILL(void) {
    unsigned char selector = 0;
    if (IN_ILL_SET_L == 0) {
        selector = LEVEL_ILL_LOW; // 照明低档
    }
    else if (IN_ILL_SET_M == 0) {
        selector = LEVEL_ILL_MID; // 照明中档
    }
    else if (IN_ILL_SET_H == 0) {
        selector = LEVEL_ILL_HIGH; // 照明高档
    }
    else {
        selector = LEVEL_ILL_OFF; // 照明关闭
    }
    return selector;

}
/**
 * @brief 瞄准旋钮档位扫描
 *
 * @return unsigned char
 */
unsigned char bsp_Selector_Scan_AIM(void) {
    unsigned char selector = 0;
    if (IN_AIM_SET_L == 0) {
        selector = LEVEL_AIM_LOW; // 瞄准低档
    }
    else if (IN_AIM_SET_M == 0) {
        selector = LEVEL_AIM_MID; // 瞄准中档
    }
    else if (IN_AIM_SET_H == 0) {
        selector = LEVEL_AIM_HIGH; // 瞄准高档
    }
    else if (IN_AIM_SET_G == 0) {

        selector = LEVEL_AIM_GREEN; // 瞄准绿光档

    }
    else {
        selector = LEVEL_AIM_OFF; // 瞄准关闭
    }
    return selector;
}



